import http.cookies
import requests
import json
import time


def keep_cookie(cookie_string):
    print(cookie_string)
    result = ''
    session = requests.Session()
    cookie = http.cookies.SimpleCookie()
    cookie.load(cookie_string)
    for key, morsel in cookie.items():
        session.cookies.set(key, morsel)

    if session.cookies.keys().count("Authorization") > 1:
        session.cookies.set("Authorization", domain="", value=None)
    res = session.post("http://wechat.v2.traceint.com/index.php/graphql/", json={
        "query": 'query getUserCancleConfig { userAuth { user { holdValidate: getSchConfig(fields: "hold_validate", extra: true) } } }',
        "variables": {},
        "operationName": "getUserCancleConfig"
    })
    try:
        result = res.json()
    except json.decoder.JSONDecodeError as err:
        print("Error: %s" % err)
    if result.get("errors") and result.get("errors")[0].get("code") != 0:
        print("Session expired!")
    else:print("Session OK.")

    print("keep_cookie")
    time.sleep(60)

